<?php

namespace JsonLowCode\Form\Abstract;

use Closure;
use JsonSerializable;
use JsonLowCode\Schema;
use JsonLowCode\UI\AppForm;
use JsonLowCode\Form;

abstract class FormAbstract implements JsonSerializable
{
    protected AppForm $form;
    protected Form $build;

    protected Schema $element;

    abstract protected function booted();

    public function __construct(Form $form, Closure $closure = null)
    {
        $this->build = $form;
        $this->form = $form->form();
        $this->booted();
        !is_null($closure)&&call_user_func($closure,$this);
    }

    public function build(): Form
    {
        return $this->build;
    }

    public function form(): AppForm
    {
        return $this->form;
    }

    public function element(): Schema
    {
        return $this->element;
    }

    /**
     * @return array
     */
    public function jsonSerialize(): array
    {
        return $this->element->jsonSerialize();
    }
}
